package com.lemon.smartsafety.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.Selection;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.JavascriptInterface;
import android.widget.EditText;

import com.google.gson.Gson;
import com.lemon.smartsafety.utils.common.LoggerHelper;


/**
 * 类描述：系统打电话相关
 * 创建人：Frago...
 * 创建时间：${date} ${time}
 */
public class SystemUtil {
    private static Intent intent;
    private static String TAG = "SystemUtil";

    /**
     * 打电话
     *
     * @param phone
     */
    @JavascriptInterface
    public static void openCall(final Activity mActivity, final String phone) {

        LoggerHelper.error(SystemUtil.class, "电话号码:" + phone);

        try {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                    mActivity.startActivity(intent);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static String tranJson(Object o) {
        String result = "";

        try {
            Gson gson = new Gson();
            result = gson.toJson(o);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


    //设置输入框的指针
    public static void setEditTextPoint(EditText editText) {

        Editable etext = editText.getText();
        Selection.setSelection(etext, etext.length());
    }



    //隐藏虚拟键盘
    public static void HideKeyboard(Activity activity) {

        try {
            ((InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

        /*    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

            if (imm != null) {
                imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    //隐藏虚拟键盘
    public static void HideKeyboardForDialog(Activity activity, Dialog dialog) {

        try {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

            if (imm != null) {
                imm.hideSoftInputFromWindow(dialog.getWindow().getDecorView().getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //隐藏虚拟键盘
    public static void HideKeyboardForEdt(Activity activity, EditText editText) {

        try {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

            if (imm != null) {
                imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    //显示虚拟键盘
    public static void ShowKeyboard(View v) {

        try {
            v.setFocusable(true);
            v.setFocusableInTouchMode(true);
            v.requestFocus();
            InputMethodManager inputManager =

                    (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

            inputManager.showSoftInput(v, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

      /*  InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );

        imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);*/

    }


    //设置焦点
    public static void setFocusable(View v) {
        v.setFocusable(true);
        v.setFocusableInTouchMode(true);
        v.requestFocus();
        v.requestFocusFromTouch();

    }


    public static void ShowKeyboard(Activity activity) {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }


    public static void exitSystem() {
        try {
            // 关闭进程
            int pid = android.os.Process.myUid();
            Log.e(TAG, "killProcess: " + pid);
            android.os.Process.killProcess(pid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 退出
            Log.e(TAG, "exit");
            System.exit(0);
        }
    }

    public static float getWindowWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        float width = wm.getDefaultDisplay().getWidth();
        return width;
    }

    public static float getWindowHight(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        float height = wm.getDefaultDisplay().getHeight();

        return height;
    }


}